<template>
    <div class='mine-container'>
        <div class="mine-container-self">

            <div class="mine-container-self-avr">
                <img v-if="self.avr" :src="self.avr" alt="">
                <img v-if="!self.avr" src="@/assets/avr.jpg" alt="">
            </div>
            <div class="mine-container-self-info">
                <div class="mine-container-self-info-name">{{ self.nickName }}</div>
                <div class="mine-container-self-info-remark">{{ self.role === 1 ? '星耀会员' : '专业代练' }}</div>
            </div>
            <div class="mine-container-self-button" @click="toModifyMine">
                <van-icon name="edit" />
                <span class="mine-container-self-button-text">编辑</span>
            </div>

        </div>
        <div class="mine-container-shouru" v-if="self.role === 2">
            <div class="mine-container-shouru-item">
                <div class="mine-container-shouru-item-value">{{ count.total || 0 }}</div>
                <div class="mine-container-shouru-item-title">总接单</div>
            </div>
            <div class="mine-container-shouru-item">
                <div class="mine-container-shouru-item-value">{{ count.month || 0 }}</div>
                <div class="mine-container-shouru-item-title">本月接单</div>
            </div>
            <div class="mine-container-shouru-item">
                <div class="mine-container-shouru-item-value">{{ count.shouru.toFixed(2) || 0 }}</div>
                <div class="mine-container-shouru-item-title">总收入</div>
            </div>
            <div class="mine-container-shouru-item">
                <div class="mine-container-shouru-item-value">{{ count.yueshouru.toFixed(2) || 0 }}</div>
                <div class="mine-container-shouru-item-title">本月收入</div>
            </div>
        </div>
        <div class="mine-container-menu">
            <div class="mine-container-menu-item" @click="toAmount">
                <div class="mine-container-menu-item-logo">
                    <van-icon name="card" />
                </div>
                <div class="mine-container-menu-item-text">
                    我的钱包
                </div>
                <div class="mine-container-menu-item-arrow">
                    <van-icon name="arrow" />
                </div>
            </div>

            <div class="mine-container-menu-item" @click="toOrder" v-if="self.role === 2">
                <div class="mine-container-menu-item-logo">
                    <van-icon name="clock" />
                </div>
                <div class="mine-container-menu-item-text">
                    接单历史
                </div>
                <div class="mine-container-menu-item-arrow">
                    <van-icon name="arrow" />
                </div>
            </div>
        </div>

        <van-button round type="danger" style="width: 100%; margin-top: 30px;" @click="logout">退出登录</van-button>
    </div>
</template>

<script>
import orderApi from '@/plugins/api/order'
import dealApi from '@/plugins/api/deal'
import globalData from '@/plugins/utils/global'
import { fuzzyMatch } from '@/plugins/utils/filterArray'
import { getToday, getThisWeek, getThisMonth, getThisYear } from '@/plugins/utils/timeUtils'
export default {
    data() {
        return {
            self: this.$global.self || {},
            orders: [],
            count: {
                shouru: 0,
                yueshouru: 0,
                total: 0,
                month: 0
            },
            deals: []
        }
    },
    methods: {
        toModifyMine() {
            this.$router.push('/mine/modify')
        },
        toAmount() {
            this.$router.push('/mine/amount')
        },
        toOrder() {
            this.$router.push('/mine/order')
        },
        async getOrder() {
            const res = await orderApi.list()
            if (res.code === 200) {
                this.orders = res.data.filter(item => item.orderStatus !== 9)
            }
        },
        async getDeal() {
            const res = await dealApi.list()
            console.log(res);

            if (res.code === 200) {
                this.deals = res.data
                this.getTotal()
            }
        },

        logout() {
            localStorage.removeItem('user')
            this.$global.self = {}
            this.$router.push('/login')
        },

        getTotal() {
            console.log(111);

            if (this.self.role === 2) {

                const arr = this.orders.filter(order => {
                    const ids = order.playerId.split(',').map(id => id.trim());
                    return ids.includes(this.self.id.toString());
                })
                const time = getThisMonth()
                const month = arr.filter(item => new Date(item.createdAt).getTime() > time.start && new Date(item.createdAt).getTime() < time.end)
                const userDeal = this.deals.filter(item => item.userId === this.self.id)
                const monthDeal = userDeal.filter(item => Number(item.createTime) >= time.start && Number(item.createTime) <= time.end)
                console.log(monthDeal);

                this.count = {
                    shouru: 0,
                    yueshouru: 0,
                    total: arr.length,
                    month: month.length
                }
                for (let i = 0; i < userDeal.length; i++) {
                    this.count.shouru = Number(userDeal[i].value) + this.count.shouru
                }
                for (let j = 0; j < monthDeal.length; j++) {
                    console.log(Number(monthDeal[j].value));
                    this.count.yueshouru = Number(monthDeal[j].value) + this.count.yueshouru
                }
            }

        },

    },
    async mounted() {
        await this.getOrder()
        await this.getDeal()
    }
}
</script>

<style lang='scss' scoped>
@import '../assets/global/mixin.scss';

.mine-container {
    padding: 10px 10px;

    &-self {
        padding: 20px;
        background-color: #fff;
        border-radius: 10px;
        @include flex(row, center, flex-start);

        &-avr {
            flex: 0 0 56px;
            height: 56px;
            border-radius: 56px;
            overflow: hidden;

            img {
                width: 100%;
            }
        }

        &-info {
            flex: 1;
            height: 52px;
            margin-left: 15px;
            @include flex(column, space-between, flex-start);

            &-name {
                font-size: 16px;
                font-weight: 600;
            }

            &-remark {
                font-size: 14px;
                color: #999;
            }
        }

        &-button {
            flex: 0 0 56px;
            height: 52px;
            font-size: 13px;
            color: #1989fa;
            text-align: right;

            &-text {
                margin-left: 5px;
                letter-spacing: 1px;
            }
        }
    }

    &-shouru {
        padding: 20px;
        background-color: #fff;
        border-radius: 10px;
        margin-top: 10px;
        display: grid;
        grid-template-columns: 1fr 1fr 1fr 1fr;

        // gap: 16px;
        &-item {
            &-value {
                font-size: 20px;
                color: #1989fa;
                text-align: center;
            }

            &-title {
                font-size: 12px;
                color: #999;
                text-align: center;
                margin-top: 3px;
            }
        }

    }

    &-menu {
        width: 100%;
        border-radius: 10px;
        background-color: #fff;
        margin-top: 10px;

        &-item {
            padding: 15px;
            @include flex(row, space-between, center);
            font-size: 14px;
            color: #333;

            &:nth-child(1) {
                border-bottom: 1px solid #f4f4f4;
            }

            &-logo {
                font-size: 16px;
                color: #1989fa;
            }

            &-text {
                flex: 1;
                margin-left: 10px;
            }
        }
    }
}
</style>